package com.swak.codec;

import java.util.zip.Checksum;

import com.swak.codec.checksum.Java9Crc32C;

/**
 * Crc32c.
 * 
 * 线程安全
 * 
 * @author 618lf
 */
public class Crc32c {

	private static final ThreadLocal<Checksum> CHECKSUM_LOCAL = ThreadLocal.withInitial(() -> {
		return new Java9Crc32C();
	});

	public static final int crc32(byte[] array) {
		if (array != null) {
			return crc32(array, 0, array.length);
		}

		return 0;
	}

	public static final int crc32(byte[] array, int offset, int length) {
		Checksum crc32 = CHECKSUM_LOCAL.get();
		crc32.update(array, offset, length);
		return (int) (crc32.getValue() & 0x7FFFFFFF);
	}

	public static final int crc32(int crc, byte[] array, int length, int offset) {
		Checksum crc32 = CHECKSUM_LOCAL.get();
		crc32.update(crc);
		crc32.update(array, offset, length - offset);
		return (int) (crc32.getValue() & 0x7FFFFFFF);
	}
}